//======================================================================
//    This file is part of baijie_interface
//
//    Copyright (C) 2023 Shenzhen Baijie Technology Co., Ltd.All rights reserved.
//
//    filename : sys_setting.cpp
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//======================================================================

#include "sys_setting.h"
#include "ui_sys_setting.h"

static int g_screenWidth;
static int g_screenHeight;
static QScreen *g_screen;
static QPushButton *g_btnBacklight;
static QPushButton *g_btnTimeSet;
static QPushButton *g_btnCn;
static QPushButton *g_btnUserManual;
static QPushButton *g_btnOtg;
static QPushButton *g_btnAboutBoard;
static QLabel *g_labelCn;
static QLabel *g_labelTimeSet;
static QLabel *g_labelBacklight;
static QLabel *g_labelOtg;
static QLabel *g_labelAboutBoard;
static QLabel *g_labelUserManual;
static QString g_otgDevice;

sys_setting::sys_setting(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::sys_setting)
{
    ui->setupUi(this);

    g_screen = qApp->primaryScreen();
    g_screenWidth = g_screen->size().width();
    g_screenHeight = g_screen->size().height();

    QString strCmd = "/usr/helperboard/qt/bin/get_otg.sh";
    QProcess pro;
    pro.start("bash", QStringList() <<"-c" << strCmd);
    pro.waitForFinished(-1);
    g_otgDevice = pro.readAllStandardOutput();
    g_otgDevice.remove("\n");
    pro.close();

    setInterface();
    setBtnOtgValue();
    this->setAttribute(Qt::WA_StyledBackground,true);

    connect(g_btnBacklight,SIGNAL(clicked(bool)),this,SLOT(btn_backlight_clicked()));
    connect(g_btnTimeSet,SIGNAL(clicked(bool)),this,SLOT(btn_time_set_clicked()));
    connect(g_btnCn,SIGNAL(clicked(bool)),this,SLOT(btn_cn_clicked()));
    connect(g_btnUserManual,SIGNAL(clicked(bool)),this,SLOT(btn_user_clicked()));
    connect(g_btnOtg,SIGNAL(clicked(bool)),this,SLOT(btn_OtgHost_clicked()));
    connect(g_btnAboutBoard,SIGNAL(clicked(bool)),this,SLOT(btn_about_clicked()));

    connect(&g_backlightWg,SIGNAL(backlight_back_msg()),this,SLOT(backlight_back()));
    connect(&g_timesetWg,SIGNAL(time_set_back_msg()),this,SLOT(time_back()));
    connect(&g_userWg,SIGNAL(user_manual_back_msg()),this,SLOT(user_back()));
    connect(&g_boardWg,SIGNAL(about_board_back_msg()),this,SLOT(board_back()));
}

sys_setting::~sys_setting()
{
    delete ui;
}

void sys_setting::on_btn_ret_clicked()
{
    emit sys_back_msg();
}

void sys_setting::btn_backlight_clicked()
{
    this->hide();
    g_backlightWg.resize(g_screenWidth,g_screenHeight);
    g_backlightWg.show();
}

void sys_setting::btn_time_set_clicked()
{
    this->hide();
    g_timesetWg.resize(g_screenWidth,g_screenHeight);
    g_timesetWg.show();
    g_timesetWg.activateWindow();
}

void sys_setting::btn_cn_clicked()
{
    emit main_cn_msg();                      // Send signals, translate all the interfaces of the system
}

void sys_setting::languageReload()
{
    ui->retranslateUi(this);
    g_labelBacklight->setText(tr("Backlight"));
    g_labelTimeSet->setText(tr("Time Set"));
    g_labelCn->setText(tr("Chinese/English"));
    g_labelUserManual->setText(tr("User Manual"));
    setBtnOtgValue();
    g_labelAboutBoard->setText(tr("About Board"));
    g_timesetWg.languageReload();
    g_backlightWg.languageReload();
    g_boardWg.languageReload();
    g_userWg.languageReload();
}

void sys_setting::btn_user_clicked()
{
    this->hide();
    g_userWg.resize(g_screenWidth,g_screenHeight);
    g_userWg.show();
}

void sys_setting::btn_about_clicked()
{
    this->hide();
    g_boardWg.resize(g_screenWidth,g_screenHeight);
    g_boardWg.show();
}

void sys_setting::backlight_back()
{
    g_backlightWg.hide();
    this->show();
}

void sys_setting::time_back()
{
    g_timesetWg.hide();
    this->show();
    this->activateWindow();
}

void sys_setting::board_back()
{
    g_boardWg.hide();
    this->show();
}

void sys_setting::user_back()
{
    g_userWg.hide();
    this->show();
}

void sys_setting::btn_OtgHost_clicked()
{
    QString strOtgStatus = g_labelOtg->text();
    if(strOtgStatus == tr("OTG toggle(Slave)"))
    {
        setOtgHost();
    }
    else
    {
        setOtgSlave();
    }
}

void sys_setting::setOtgHost()
{
    QString strCmd;
    if(!g_otgDevice.isEmpty())
        strCmd = QString("cat /sys/bus/platform/drivers/otg\" \"manager/%1/usb_host").arg(g_otgDevice);
    else
        strCmd = "cat /sys/bus/platform/drivers/otg\" \"manager/usbc0/usb_host";
    QProcess pro;
    pro.start("bash", QStringList() <<"-c" << strCmd);
    pro.waitForFinished(-1);
    QString strResult = pro.readAllStandardOutput();
    pro.close();
    if(!strResult.isEmpty())
    {
        g_labelOtg->setText(tr("OTG toggle(Host)"));
    }
    else
    {
        QMessageBox mesg(QMessageBox::Information,
                         tr("QMessageBox::information()"),
                         tr("Set failed!"),
                         0,this);
        mesg.addButton(tr("OK"),QMessageBox::YesRole);
        mesg.move(g_screenWidth/3,g_screenHeight/3);
        mesg.exec();
    }
}

void sys_setting::setOtgSlave()
{
    QString strCmd;
    if(!g_otgDevice.isEmpty())
        strCmd = QString("cat /sys/bus/platform/drivers/otg\" \"manager/%1/usb_device").arg(g_otgDevice);
    else
        strCmd = "cat /sys/bus/platform/drivers/otg\" \"manager/usbc0/usb_device";
    QProcess pro;
    pro.start("bash", QStringList() <<"-c" << strCmd);
    pro.waitForFinished(-1);
    QString strResult = pro.readAllStandardOutput();
    pro.close();
    if(!strResult.isEmpty())
    {
        g_labelOtg->setText(tr("OTG toggle(Slave)"));
    }
    else
    {
        QMessageBox mesg(QMessageBox::Information,
                         tr("QMessageBox::information()"),
                         tr("Set failed!"),
                         0,this);
        mesg.addButton(tr("OK"),QMessageBox::YesRole);
        mesg.move(g_screenWidth/3,g_screenHeight/3);
        mesg.exec();
    }
}

void sys_setting::setBtnOtgValue()
{
    QString strCmd;
    if(!g_otgDevice.isEmpty())
        strCmd = QString("cat /sys/bus/platform/drivers/otg\" \"manager/%1/otg_role").arg(g_otgDevice);
    else
        strCmd = "cat /sys/bus/platform/drivers/otg\" \"manager/usbc0/otg_role";
    QProcess pro;
    pro.start("bash", QStringList() <<"-c" << strCmd);
    pro.waitForFinished(-1);
    QString strResult = pro.readAllStandardOutput();
    pro.close();
    if(strResult.contains("device",Qt::CaseInsensitive))
    {
        g_labelOtg->setText(tr("OTG toggle(Slave)"));
    }
    else
    {
        g_labelOtg->setText(tr("OTG toggle(Host)"));
    }
}

void sys_setting::setInterface()
{
    this->setGeometry(0,0,g_screenWidth,g_screenHeight);
    int widgetHeight = g_screenHeight*5/6;
    int widgetWidth = g_screenWidth;
    g_btnBacklight = new QPushButton(ui->widget);
    g_btnBacklight->setStyleSheet("border-image: url(:/button_image/backlight.svg);"
                                  "background-color:transparent;");
    g_btnBacklight->setGeometry(widgetWidth*1/14+(widgetWidth*4/14-widgetHeight*12/56)/2,widgetHeight*7/56,widgetHeight*12/56,widgetHeight*12/56);
    g_btnBacklight->show();
    g_btnTimeSet = new QPushButton(ui->widget);
    g_btnTimeSet->setStyleSheet("border-image: url(:/button_image/timeset.svg);"
                                "background-color:transparent;");
    g_btnTimeSet->setGeometry(widgetWidth*5/14+(widgetWidth*4/14-widgetHeight*12/56)/2,widgetHeight*7/56,widgetHeight*12/56,widgetHeight*12/56);
    g_btnTimeSet->show();
    g_btnCn = new QPushButton(ui->widget);
    g_btnCn->setStyleSheet("border-image: url(:/button_image/cn.svg);"
                           "background-color:transparent;");
    g_btnCn->setGeometry(widgetWidth*9/14+(widgetWidth*4/14-widgetHeight*12/56)/2,widgetHeight*7/56,widgetHeight*12/56,widgetHeight*12/56);
    g_btnCn->show();

    g_btnUserManual = new QPushButton(ui->widget);
    g_btnUserManual->setStyleSheet("border-image: url(:/button_image/manual.svg);"
                                   "background-color:transparent;");
    g_btnUserManual->setGeometry(widgetWidth*1/14+(widgetWidth*4/14-widgetHeight*12/56)/2,widgetHeight*31/56,widgetHeight*12/56,widgetHeight*12/56);
    g_btnUserManual->show();
    g_btnOtg = new QPushButton(ui->widget);
    g_btnOtg->setStyleSheet("border-image: url(:/button_image/otg.svg);"
                            "background-color:transparent;");
    g_btnOtg->setGeometry(widgetWidth*5/14+(widgetWidth*4/14-widgetHeight*12/56)/2,widgetHeight*31/56,widgetHeight*12/56,widgetHeight*12/56);
    g_btnOtg->show();
    g_btnAboutBoard = new QPushButton(ui->widget);
    g_btnAboutBoard->setStyleSheet("border-image: url(:/button_image/about.svg);"
                                   "background-color:transparent;");
    g_btnAboutBoard->setGeometry(widgetWidth*9/14+(widgetWidth*4/14-widgetHeight*12/56)/2,widgetHeight*31/56,widgetHeight*12/56,widgetHeight*12/56);
    g_btnAboutBoard->show();

    g_labelBacklight = new QLabel(ui->widget);
    g_labelBacklight->setAlignment(Qt::AlignCenter);
    g_labelBacklight->setText(tr("Backlight"));
    g_labelBacklight->setGeometry(widgetWidth*1/14,widgetHeight*21/56,widgetWidth*4/14,widgetHeight*4/56);
    g_labelBacklight->show();
    g_labelTimeSet = new QLabel(ui->widget);
    g_labelTimeSet->setAlignment(Qt::AlignCenter);
    g_labelTimeSet->setText(tr("Time Set"));
    g_labelTimeSet->setGeometry(widgetWidth*5/14,widgetHeight*21/56,widgetWidth*4/14,widgetHeight*4/56);
    g_labelTimeSet->show();
    g_labelCn = new QLabel(ui->widget);
    g_labelCn->setAlignment(Qt::AlignCenter);
    g_labelCn->setText(tr("Chinese/English"));
    g_labelCn->setGeometry(widgetWidth*9/14,widgetHeight*21/56,widgetWidth*4/14,widgetHeight*4/56);
    g_labelCn->show();

    g_labelUserManual = new QLabel(ui->widget);
    g_labelUserManual->setAlignment(Qt::AlignCenter);
    g_labelUserManual->setText(tr("User Manual"));
    g_labelUserManual->setGeometry(widgetWidth*1/14,widgetHeight*45/56,widgetWidth*4/14,widgetHeight*4/56);
    g_labelUserManual->show();
    g_labelOtg = new QLabel(ui->widget);
    g_labelOtg->setAlignment(Qt::AlignCenter);
    g_labelOtg->setText(tr("OTG toggle(Slave)"));
    g_labelOtg->setGeometry(widgetWidth*5/14,widgetHeight*45/56,widgetWidth*4/14,widgetHeight*4/56);
    g_labelOtg->show();
    g_labelAboutBoard = new QLabel(ui->widget);
    g_labelAboutBoard->setAlignment(Qt::AlignCenter);
    g_labelAboutBoard->setText(tr("About Board"));
    g_labelAboutBoard->setGeometry(widgetWidth*9/14,widgetHeight*45/56,widgetWidth*4/14,widgetHeight*4/56);
    g_labelAboutBoard->show();

    qreal realX = g_screen->physicalDotsPerInchX();
    qreal realWidth = g_screenWidth / realX * 2.54;
    QFont font;
    if(realWidth < 15)
    {
        font.setPointSize(12);
    }
    else if (realWidth < 18)
    {
        font.setPointSize(14);
    }
    else
    {
        font.setPointSize(17);
    }
    g_labelCn->setFont(font);
    g_labelTimeSet->setFont(font);
    g_labelBacklight->setFont(font);
    g_labelOtg->setFont(font);
    g_labelAboutBoard->setFont(font);
    g_labelUserManual->setFont(font);
    ui->lbl_system->setFont(font);
}
